package com.agentpad;

import java.util.List;
import android.app.Activity;
import android.content.ContentValues;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.widget.AbsListView;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.agentpad.util.*;

public class MainActivity extends Activity {
	private HouseDbHelper dbHelper = new HouseDbHelper(this);
	private static final int IMAGE_CAPTURE = 0; 
	private Uri imageUri; 
	private HousePictureAdapter picAdapter = null;
	private HouseListAdapter listAdapter = null; 
	private int selectedPos = -1;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        dbHelper.open();
        List<House> houses = dbHelper.getHouses();
        
        final ListView lv = (ListView)findViewById(R.id.lstHouses); 
//        lv.setChoiceMode(AbsListView.CHOICE_MODE_SINGLE);
        listAdapter = new HouseListAdapter(this, houses);
        lv.setAdapter(listAdapter);
        lv.setOnItemClickListener(new OnItemClickListener() {
        	@Override
        	public  void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//              view.setSelected(true);
              
              HouseListAdapter adapter = (HouseListAdapter)parent.getAdapter();
              House house = adapter.getHouse(position);
              
              EditText edtHouseName = (EditText)findViewById(R.id.edtHouseName);
              EditText edtHouseAddr = (EditText)findViewById(R.id.edtHouseAddr);
              EditText edtHouseStruct = (EditText)findViewById(R.id.edtHouseStruct);
              EditText txtHousePic = (EditText)findViewById(R.id.txtHousePic);
              edtHouseName.setText(house.getName());
              edtHouseAddr.setText(house.getAddr());
              edtHouseStruct.setText(house.getStruct());
              txtHousePic.setText(house.getPic());
              
              picAdapter.clear();
              if(house.getPic() != null && !"".equals(house.getPic()))
            	  picAdapter.addPic(house.getPic());
              
              selectedPos = position;
            }
          });

        Button btnAddHouse = (Button)findViewById(R.id.btnAddHouse);
        btnAddHouse.setOnClickListener(new OnClickListener() {
		        @Override
		        public void onClick(View v) {
		        	addHouse();
		        }});

        GridView gridview = (GridView) findViewById(R.id.gridView42);    
        picAdapter = new HousePictureAdapter(this);
        gridview.setAdapter(picAdapter);
        gridview.setOnItemClickListener(new OnItemClickListener() {        
        	public void onItemClick(AdapterView<?> parent, View v, int position, long id) {            
        		Toast.makeText(getApplicationContext(), "" + position, Toast.LENGTH_SHORT).show();        
        	}    
        });
        
        Button btnShoot = (Button)findViewById(R.id.btnShoot);
        btnShoot.setOnClickListener(new OnClickListener() {
		        @Override
		        public void onClick(View v) {
		        	takePicture();
		        }});

        Button btnSave = (Button)findViewById(R.id.btnSave);
        btnSave.setOnClickListener(new OnClickListener() {
		        @Override
		        public void onClick(View v) {
		        	save();
		        }});

    }
    
    private void addHouse()
    {
    	EditText edtHouseName = (EditText)findViewById(R.id.edtHouseNameToAdd);
    	if(edtHouseName.getText() != null && !"".equals(edtHouseName.getText().toString()) )
    	{
    		dbHelper.insertHouse(edtHouseName.getText().toString(), "δ֪", "δ֪", null);
    		edtHouseName.setText(null);
    		ListView lv = (ListView)findViewById(R.id.lstHouses);
    		lv.invalidate();
//    		listAdapter.notifyDataSetChanged();
    	}
    }
    
    private void save()
    {
    	if(selectedPos < 0)
    		return;
    	
        ListView lv = (ListView)findViewById(R.id.lstHouses);
        HouseListAdapter adapter = (HouseListAdapter)lv.getAdapter();
        House house = (House)adapter.getItem(selectedPos);
        EditText edtHouseName = (EditText)findViewById(R.id.edtHouseName);
        EditText edtHouseAddr = (EditText)findViewById(R.id.edtHouseAddr);
        EditText edtHouseStruct = (EditText)findViewById(R.id.edtHouseStruct);
        EditText txtHousePic = (EditText)findViewById(R.id.txtHousePic);
        dbHelper.updateHouse(house.getId(), edtHouseName.getText().toString(), edtHouseAddr.getText().toString(), edtHouseStruct.getText().toString(), txtHousePic.getText().toString());
        Toast.makeText(getApplicationContext(), R.string.Success, Toast.LENGTH_SHORT).show();
        house.setName(edtHouseName.getText().toString());
        house.setAddr(edtHouseAddr.getText().toString());
        house.setStruct(edtHouseStruct.getText().toString());
        house.setPic(txtHousePic.getText().toString());
        lv.invalidate();
    }
    
    public void takePicture() { 
    	// Log.d("ANDRO_CAMERA", "Starting camera on the phone..."); 
    	String fileName = "testphoto.jpg"; 
    	ContentValues values = new ContentValues(); 
    	values.put(MediaStore.Images.Media.TITLE, fileName); 
    	values.put(MediaStore.Images.Media.DESCRIPTION, "Image capture by camera"); 
    	values.put(MediaStore.Images.Media.MIME_TYPE, "image/jpeg"); 
    	imageUri = getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values); 
    	Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); 
    	intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri); 
    	intent.putExtra(MediaStore.EXTRA_VIDEO_QUALITY, 1); 
    	startActivityForResult(intent, IMAGE_CAPTURE); 
    } 

	protected void onActivityResult(int requestCode, int resultCode, Intent data) 
	{ 
		if (requestCode == IMAGE_CAPTURE) 
		{ 
			if (resultCode == RESULT_OK)
			{ 
				picAdapter.addPic(imageUri);
		        EditText txtHousePic = (EditText)findViewById(R.id.txtHousePic);
		        if(txtHousePic.getText().toString() == null || "".equals(txtHousePic.getText().toString()))
		        	txtHousePic.setText(imageUri.toString());
		        else
		        	txtHousePic.setText(txtHousePic.getText().toString() + ";" + imageUri.toString());
		        
		        save();
			} 
		}
	}
}
